﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TangoFamilyAndGuide.Site
{
    public partial class CastingArtistsOfferCrear : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Funciones.SessionTimeOut(Session))
                {
                    Response.Redirect("~/SessionTimeout.aspx");
                }
                else
                {
                    Entidades.Sesion sesion = (Entidades.Sesion)Session["Sesion"];
                    List<Entidades.Permiso> permisoHabilitado = sesion.Usuario.Permisos.FindAll(delegate(Entidades.Permiso p)
                    {
                        return p.TipoPermiso.Id == "OperCastingArtistsOffer" && p.Estado == "Active";
                    });
                    if (permisoHabilitado.Count == 0)
                    {
                        Response.Redirect("~/Home.aspx");
                    }

                    StyleCheckBoxList.DataSource = Entidades.StylesCastingOffer.StyleCastingOffer.Lista();
                    NombreTextBox.Focus();
                    DataBind();
                }
            }
        }
        private string GenerarStyles()
        {
            string texto = "";
            foreach (ListItem item in StyleCheckBoxList.Items)
            {
                texto += item.Value + "-";
            }
            if (texto != "")
            {
                texto = texto.Substring(0, texto.Length - 1);
            }
            return texto;
        }
        protected void AceptarButton_Click(object sender, EventArgs e)
        {
            if (Funciones.SessionTimeOut(Session))
            {
                Response.Redirect("~/SessionTimeout.aspx");
            }
            else
            {
                Entidades.Sesion sesion = (Entidades.Sesion)Session["Sesion"];
                Entidades.CastingArtistsOffer castingArtistsOffer = new Entidades.CastingArtistsOffer();
                try
                {
                    castingArtistsOffer.Usuario.Id = sesion.Usuario.Id;
                    castingArtistsOffer.Nombre = NombreTextBox.Text.Trim();
                    castingArtistsOffer.Descripcion = DescripcionTextBox.Text.Trim();
                    castingArtistsOffer.Pais = Domicilio.Pais;
                    castingArtistsOffer.Provincia = Domicilio.Provincia;
                    castingArtistsOffer.Localidad = Domicilio.Localidad;
                    castingArtistsOffer.Email = Domicilio.Email;
                    castingArtistsOffer.Telefono = Domicilio.Telefono;
                    castingArtistsOffer.WebSite = WebSiteTextBox.Text;
                    castingArtistsOffer.Facebook = FacebookTextBox.Text;
                    castingArtistsOffer.YouTube = YouTubeTextBox.Text;
                    castingArtistsOffer.Estilo = GenerarStyles();
                    castingArtistsOffer.Ranking = 0;

                    if (RN.CastingArtistsOffer.ComprobarNombreCastingArtistsOffer(castingArtistsOffer.Nombre, sesion))
                    {
                        MensajeLabel.Text = "There is already a Casting Offers for Artist with a same name, modify the data. ";
                    }
                    else
                    {
                        int IdCastingArtistsOffer = 0;
                        RN.CastingArtistsOffer.Crear(out IdCastingArtistsOffer, castingArtistsOffer, sesion);

                        NombreTextBox.Enabled = false;
                        DescripcionTextBox.Enabled = false;
                        Domicilio.Enabled = false;
                        WebSiteTextBox.Enabled = false;
                        FacebookTextBox.Enabled = false;
                        YouTubeTextBox.Enabled = false;
                        StyleCheckBoxList.Enabled = false;
                        AceptarButton.Enabled = false;
                        SalirButton.Text = "Salir";

                        Funciones.PersonalizarControlesMaster(Master, true, sesion);
                        MensajeLabel.Text = "There was successfully. ";
                        if (IdCastingArtistsOffer != 0)
                        {
                            MensajeLabel.Text = MensajeLabel.Text + "Id: " + IdCastingArtistsOffer;
                        }
                        castingArtistsOffer.Id = IdCastingArtistsOffer;
                        Session["CastingArtistsOffer"] = castingArtistsOffer;
                        Response.Redirect("~/CastingArtistsOfferImagenes.aspx");
                    }
                }
                catch (Exception ex)
                {
                    MensajeLabel.Text = EX.Funciones.Detalle(ex);
                    return;
                }
            }
        }
    }
}